/**
* @file string_utils.cpp
*
* Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "string_utils.h"

namespace acl {
void StringUtils::Split(const std::string &str, const char_t delim, std::vector<std::string> &elems)
{
    elems.clear();
    if (str.empty()) {
        elems.emplace_back("");
        return;
    }

    std::stringstream ss(str);
    std::string item;

    while (getline(ss, item, delim)) {
        elems.push_back(item);
    }

    const auto strSize = str.size();
    if ((strSize > 0U) && (str[strSize - 1U] == delim)) {
        elems.emplace_back("");
    }
}

bool StringUtils::IsDigit(const std::string &str)
{
    if (str.empty()) {
        return false;
    }

    for (const char_t &c : str) {
        if (isdigit(static_cast<int32_t>(c)) == 0) {
            return false;
        }
    }
    return true;
}

void StringUtils::Trim(std::string &str)
{
    if (str.empty()) {
        return;
    }
    (void)str.erase(0U, str.find_first_not_of(" "));
    (void)str.erase(str.find_last_not_of(" ") + 1U);
}

void StringUtils::Strip(std::string &str, const std::string &sep)
{
    if (str.empty()) {
        return;
    }
    (void) str.erase(0U, str.find_first_not_of(sep));
    (void) str.erase(str.find_last_not_of(sep) + 1U);
}
} // namespace acl